package com.ruoyi.web.controller.business;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.ruoyi.common.core.domain.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import org.apache.commons.lang3.StringUtils;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.SysAppUser;
import com.ruoyi.business.service.ISysAppUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.domain.BaseEntity;
import org.springframework.web.multipart.MultipartFile;


/**
 * 小程序用户Controller
 *
 * @author ruoyi
 * @date 2023-11-14
 */
@RestController
@RequestMapping("/appUser")
@Api(tags = "小程序用户")
public class SysAppUserController extends BaseController {
    @Autowired
    private ISysAppUserService sysAppUserService;

    /**
     * 查询小程序用户分页列表
     */
    @GetMapping("/pageList")
    @ApiOperation(value = "小程序用户分页查询")
    public R<IPage<SysAppUser>> list(SysAppUser sysAppUser,Integer pageNum,Integer pageSize){
    IPage<SysAppUser> list = getQw(sysAppUser)
        .page(new Page<SysAppUser>(pageNum, pageSize));
    return R.ok(list);
    }


    /**
     * 导出小程序用户列表
     */
    @Log(title = "小程序用户" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "小程序用户导出")
    public void export(HttpServletResponse response, SysAppUser sysAppUser) {
        List<SysAppUser> list = getQw(sysAppUser).list();
        ExcelUtil<SysAppUser> util = new ExcelUtil<SysAppUser>(SysAppUser. class);
        util.exportExcel(response, list, "小程序用户数据");
    }
    @PostMapping("/exportTemplate")
    @ApiOperation(value = "小程序用户导出")
    public void exportTemplate(HttpServletResponse response, SysAppUser sysAppUser) {
        List<SysAppUser> list = new ArrayList<>();
        ExcelUtil<SysAppUser> util = new ExcelUtil<SysAppUser>(SysAppUser. class);
        util.exportExcel(response, list, "小程序用户数据");
    }

    /**
     * 获取小程序用户详细信息
     */
    @GetMapping(value = "/{appUserId}")
    @ApiOperation(value = "获取小程序用户详细信息")
    public R getInfo(@PathVariable("appUserId") Long appUserId) {
        return R.ok(sysAppUserService.getById(appUserId));
    }

    /**
     * 新增小程序用户
     */
    @Log(title = "小程序用户" , businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增小程序用户")
    public R add(@RequestBody SysAppUser sysAppUser) {
        sysAppUser.setType("学员");
        return R.ok(sysAppUserService.save(sysAppUser));
    }

    /**
     * 修改小程序用户
     */
    @Log(title = "小程序用户" , businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改小程序用户")
    public R edit(@RequestBody SysAppUser sysAppUser) {
        return R.ok(sysAppUserService.updateById(sysAppUser));
    }


    @PostMapping("/importData")
    @ApiOperation(value = "导入")
    public R importData(MultipartFile file) throws Exception
    {
        ExcelUtil<SysAppUser> util = new ExcelUtil<SysAppUser>(SysAppUser.class);
        List<SysAppUser> sysAppUserList = util.importExcel(file.getInputStream());
        sysAppUserList.stream().forEach(item->item.setType("学员"));
        sysAppUserService.saveBatch(sysAppUserList);
        return R.ok();
    }

    /**
     * 删除小程序用户
     */
    @Log(title = "小程序用户" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{appUserIds}")
    @ApiOperation(value = "删除小程序用户")
    public R remove(@PathVariable Long[] appUserIds) {
        SysAppUser sysAppUser = sysAppUserService.getById(appUserIds[0]);
        if (StringUtils.isNotBlank(sysAppUser.getOpenid())){
            sysAppUser.setType("普通会员");
            sysAppUserService.updateById(sysAppUser);
            return R.ok();
        }
        return R.ok(sysAppUserService.removeByIds(Arrays.asList(appUserIds)));
    }


    private LambdaQueryChainWrapper<SysAppUser> getQw(SysAppUser sysAppUser){
          return  sysAppUserService.lambdaQuery()
                .orderByDesc(BaseEntity::getCreateTime)
                .like(StringUtils.isNotBlank(sysAppUser.getOpenid()),SysAppUser::getOpenid,sysAppUser.getOpenid())
                .like(StringUtils.isNotBlank(sysAppUser.getRealName()),SysAppUser::getRealName,sysAppUser.getRealName())
                .eq(StringUtils.isNotBlank(sysAppUser.getType()),SysAppUser::getType,sysAppUser.getType())
                .like(StringUtils.isNotBlank(sysAppUser.getNickName()),SysAppUser::getNickName,sysAppUser.getNickName())
                .like(StringUtils.isNotBlank(sysAppUser.getPhonenumber()),SysAppUser::getPhonenumber,sysAppUser.getPhonenumber())
                .like(StringUtils.isNotBlank(sysAppUser.getAvatar()),SysAppUser::getAvatar,sysAppUser.getAvatar())
                .like(StringUtils.isNotBlank(sysAppUser.getStatus()),SysAppUser::getStatus,sysAppUser.getStatus())
                .eq(sysAppUser.getMoney()!= null,SysAppUser::getMoney,sysAppUser.getMoney())
                .in(CollUtil.isNotEmpty(sysAppUser.getIds()), SysAppUser::getAppUserId,sysAppUser.getIds())
        ;

    }
}
